package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityLanguageBinding;
import com.apex.aolonfitdemo.dialog.LanguageDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.LanguageCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleDeviceLanguage;
import com.cyber.bridge.module.listener.OnCyberLanguageListener;

import java.util.List;


public class LanguageActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;

    private LanguageDialog languageDialog;
    private String tempLanguage;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                if (msg.arg1 == 0) {
                    activityLanguageBinding.language.setText(getString(R.string.language_english));
                } else if (msg.arg1 == 1) {
                    activityLanguageBinding.language.setText(getString(R.string.language_english));
                } else if (msg.arg1 == 2) {
                    activityLanguageBinding.language.setText(getString(R.string.language_chinese_simplifid));
                } else if (msg.arg1 == 3) {
                    activityLanguageBinding.language.setText(getString(R.string.language_japanese));
                } else if (msg.arg1 == 4) {
                    activityLanguageBinding.language.setText(getString(R.string.language_korean));
                } else if (msg.arg1 == 5) {
                    activityLanguageBinding.language.setText(getString(R.string.language_deutsch));
                } else if (msg.arg1 == 6) {
                    activityLanguageBinding.language.setText(getString(R.string.language_francais));
                } else if (msg.arg1 == 7) {
                    activityLanguageBinding.language.setText(getString(R.string.language_spanish));
                } else if (msg.arg1 == 8) {
                    activityLanguageBinding.language.setText(getString(R.string.language_arabic));
                } else if (msg.arg1 == 9) {
                    activityLanguageBinding.language.setText(getString(R.string.language_russian));
                } else if (msg.arg1 == 10) {
                    activityLanguageBinding.language.setText(getString(R.string.language_chinese_traditional));
                } else if (msg.arg1 == 11) {
                    activityLanguageBinding.language.setText(getString(R.string.language_italiano));
                } else if (msg.arg1 == 12) {
                    activityLanguageBinding.language.setText(getString(R.string.language_portuguese));
                } else if (msg.arg1 == 13) {
                    activityLanguageBinding.language.setText(getString(R.string.language_dutch));
                } else if (msg.arg1 == 14) {
                    activityLanguageBinding.language.setText(getString(R.string.language_polski));
                } else if (msg.arg1 == 15) {
                    activityLanguageBinding.language.setText(getString(R.string.language_swedish));
                } else if (msg.arg1 == 16) {
                    activityLanguageBinding.language.setText(getString(R.string.language_finnish));
                } else if (msg.arg1 == 17) {
                    activityLanguageBinding.language.setText(getString(R.string.language_danish));
                } else if (msg.arg1 == 18) {
                    activityLanguageBinding.language.setText(getString(R.string.language_norwegian));
                } else if (msg.arg1 == 19) {
                    activityLanguageBinding.language.setText(getString(R.string.language_hungarian));
                } else if (msg.arg1 == 20) {
                    activityLanguageBinding.language.setText(getString(R.string.language_czech));
                } else if (msg.arg1 == 22) {
                    activityLanguageBinding.language.setText(getString(R.string.language_romanian));
                } else if (msg.arg1 == 23) {
                    activityLanguageBinding.language.setText(getString(R.string.language_slovak));
                } else if (msg.arg1 == 25) {
                    activityLanguageBinding.language.setText(getString(R.string.language_indonesia));
                } else if (msg.arg1 == 26) {
                    activityLanguageBinding.language.setText(getString(R.string.language_thai));
                } else if (msg.arg1 == 27) {
                    activityLanguageBinding.language.setText(getString(R.string.language_ukrainian));
                } else if (msg.arg1 == 28) {
                    activityLanguageBinding.language.setText(getString(R.string.language_vietnamese));
                } else if (msg.arg1 == 38) {
                    activityLanguageBinding.language.setText(getString(R.string.language_melayu));
                }


            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityLanguageBinding.language.setText(tempLanguage);
            } else if (msg.what == 43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(LanguageActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityLanguageBinding activityLanguageBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLanguageBinding = ActivityLanguageBinding.inflate(getLayoutInflater());
        setContentView(activityLanguageBinding.getRoot());
        activityLanguageBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityLanguageBinding.tool);
        getSupportActionBar().setTitle(null);
        activityLanguageBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnLanguageListener(new OnCyberLanguageListener() {
            @Override
            public void queryLanguage(int i) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setLanguage(boolean b) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    if (b) {
                        message.what = 0x42;
                    } else {
                        message.what = 0x43;
                    }

                    mHandler.sendMessage(message);
                }
            }
        });
        AolonFitManager.getInstance().queryLanguage();

        activityLanguageBinding.language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (languageDialog == null) {
                    languageDialog = new LanguageDialog(LanguageActivity.this);
                    languageDialog.setSelectListener(new LanguageDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {

                            if (sex.equalsIgnoreCase(getString(R.string.language_english))) {
                                AolonFitManager.getInstance().setLanguage(1);

                            } else if (sex.equalsIgnoreCase(getString(R.string.language_chinese_simplifid))) {
                                AolonFitManager.getInstance().setLanguage(2);

                            } else if (sex.equalsIgnoreCase(getString(R.string.language_japanese))) {
                                AolonFitManager.getInstance().setLanguage(3);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_korean))) {
                                AolonFitManager.getInstance().setLanguage(4);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_deutsch))) {
                                AolonFitManager.getInstance().setLanguage(5);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_francais))) {
                                AolonFitManager.getInstance().setLanguage(6);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_spanish))) {
                                AolonFitManager.getInstance().setLanguage(7);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_arabic))) {
                                AolonFitManager.getInstance().setLanguage(8);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_russian))) {
                                AolonFitManager.getInstance().setLanguage(9);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_chinese_traditional))) {
                                AolonFitManager.getInstance().setLanguage(10);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_italiano))) {
                                AolonFitManager.getInstance().setLanguage(11);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_portuguese))) {
                                AolonFitManager.getInstance().setLanguage(12);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_dutch))) {
                                AolonFitManager.getInstance().setLanguage(13);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_polski))) {
                                AolonFitManager.getInstance().setLanguage(14);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_swedish))) {
                                AolonFitManager.getInstance().setLanguage(15);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_finnish))) {
                                AolonFitManager.getInstance().setLanguage(16);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_danish))) {
                                AolonFitManager.getInstance().setLanguage(17);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_norwegian))) {
                                AolonFitManager.getInstance().setLanguage(18);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_hungarian))) {
                                AolonFitManager.getInstance().setLanguage(19);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_czech))) {
                                AolonFitManager.getInstance().setLanguage(20);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_bulgarian))) {
                                AolonFitManager.getInstance().setLanguage(1);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_romanian))) {
                                AolonFitManager.getInstance().setLanguage(22);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_slovak))) {
                                AolonFitManager.getInstance().setLanguage(23);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_latvian))) {
                                AolonFitManager.getInstance().setLanguage(1);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_indonesia))) {
                                AolonFitManager.getInstance().setLanguage(25);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_thai))) {
                                AolonFitManager.getInstance().setLanguage(26);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_ukrainian))) {
                                AolonFitManager.getInstance().setLanguage(27);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_vietnamese))) {
                                AolonFitManager.getInstance().setLanguage(28);
                            } else if (sex.equalsIgnoreCase(getString(R.string.language_melayu))) {
                                AolonFitManager.getInstance().setLanguage(38);
                            } else {
                                AolonFitManager.getInstance().setLanguage(1);
                            }
                            tempLanguage = sex;

                        }
                    });
                }
                if (!languageDialog.isShowing()) {
                    languageDialog.show();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (languageDialog != null) {
            languageDialog.dismiss();
            languageDialog.destroyDialog();
            languageDialog = null;
        }
        super.onDestroy();
    }
}
